import { assign } from 'lodash';
import { AnyAction } from 'redux';
import LESSON_ACTIONS from 'constants/lesson';
import PAGE_ACTIONS from 'constants/page';
import LEVEL_ACTIONS from 'constants/level';
import TEST_ACTIONS from 'constants/test';

const initialState: Record<string, any> = {
  lessonList: [],
  hasChanged: false,
};

export default (state = initialState, action: AnyAction) => {
  switch (action.type) {
    case LESSON_ACTIONS.FETCH_LIST_SUCCESS:
      return assign({}, state, {
        lessonList: action.response.data.items,
      });

    case LESSON_ACTIONS.UPDATE_SUCCESS:
    case LESSON_ACTIONS.SAVE_COVER_SUCCESS:
    case LESSON_ACTIONS.SAVE_AUDIO_SUCCESS:
    case LESSON_ACTIONS.SORT_PAGE_SUCCESS:
    case LESSON_ACTIONS.SORT_TEST_SUCCESS:
    case PAGE_ACTIONS.SAVE_BOOK_GUIDE_SUCCESS:
    case PAGE_ACTIONS.CREATE_SUCCESS:
    case PAGE_ACTIONS.UPDATE_SUCCESS:
    case PAGE_ACTIONS.REMOVE_SUCCESS:
    case LEVEL_ACTIONS.CREATE_SUCCESS:
    case LEVEL_ACTIONS.REMOVE_SUCCESS:
    case LEVEL_ACTIONS.UPDATE_SUCCESS:
    case TEST_ACTIONS.CREATE_SUCCESS:
    case TEST_ACTIONS.REMOVE_SUCCESS:
    case TEST_ACTIONS.UPDATE_SUCCESS:
      return assign({}, state, {
        hasChanged: true,
      });

    case LESSON_ACTIONS.UPDATE_PUBLISH_STATUS_SUCCESS:
      return assign({}, state, {
        hasChanged: false,
      });

    default:
      return state;
  }
};
